
#include "register/op_def_registry.h"
#include "three_nn_tiling.h"
#include "tiling_common.h"

namespace optiling {
static ge::graphStatus TilingFunc(gert::TilingContext *context) {
  ThreeNNTilingData tiling;
  common_tiling_set_kernel_vec(tiling, context);

  auto xyz1_shape = context->GetInputShape(0)->GetStorageShape();
  auto xyz2_shape = context->GetInputShape(1)->GetStorageShape();
  tiling.set_b(xyz1_shape.GetDim(0));
  tiling.set_n(xyz1_shape.GetDim(1));
  auto m = xyz2_shape.GetDim(1);
  tiling.set_m(m);

  auto dtype = context->GetInputDesc(0)->GetDataType();
  auto dtypeSize = GetSizeByDataType(dtype);

  COMMON_TILING_GET_PLATFORM;
  COMMON_TILING_GET_UB_SIZE;

  ub_size -= 1024;
  auto tileLength = int32_t(float(ub_size) / 4.0f) / 256 * 256 / dtypeSize;
  tiling.set_tileLength(tileLength);

  tiling_zero_workspace(context);
  tiling_save_to_buffer(tiling, context);

  std::cout << "tileLength = " << tiling.get_tileLength() << std::endl;
  std::cout << "b = " << tiling.get_b() << std::endl;
  std::cout << "n = " << tiling.get_n() << std::endl;
  std::cout << "m = " << tiling.get_m() << std::endl;

  return ge::GRAPH_SUCCESS;
}
} // namespace optiling

namespace ge {
static ge::graphStatus InferShape(gert::InferShapeContext *context) {
  const gert::Shape *x1_shape = context->GetInputShape(0);
  gert::Shape *y_shape = context->GetOutputShape(0);
  *y_shape = *x1_shape;
  return GRAPH_SUCCESS;
}
} // namespace ge

namespace ops {
class ThreeNN : public OpDef {
public:
  explicit ThreeNN(const char *name) : OpDef(name) {
    this->Input("xyz1")
        .ParamType(REQUIRED)
        .DataType({ge::DT_FLOAT})
        .Format({ge::FORMAT_ND})
        .UnknownShapeFormat({ge::FORMAT_ND});
    this->Input("xyz2")
        .ParamType(REQUIRED)
        .DataType({ge::DT_FLOAT})
        .Format({ge::FORMAT_ND})
        .UnknownShapeFormat({ge::FORMAT_ND});
    this->Output("dist")
        .ParamType(REQUIRED)
        .DataType({ge::DT_FLOAT})
        .Format({ge::FORMAT_ND})
        .UnknownShapeFormat({ge::FORMAT_ND});
    this->Output("indices")
        .ParamType(REQUIRED)
        .DataType({ge::DT_INT32})
        .Format({ge::FORMAT_ND})
        .UnknownShapeFormat({ge::FORMAT_ND});

    this->SetInferShape(ge::InferShape);

    this->AICore().SetTiling(optiling::TilingFunc);
    this->AICore().AddConfig("ascend310b");
  }
};

OP_ADD(ThreeNN);
} // namespace ops
